<template>
  <view class="container">
    <view class="header">
      <text class="title">Token调试页面</text>
    </view>
    
    <view class="content">
      <button class="btn" @click="testApiCall">测试API调用</button>
      <button class="btn" @click="checkStorage">检查本地存储</button>
      <button class="btn" @click="clearStorage">清除本地存储</button>
      
      <view class="info-section">
        <text class="info-title">调试信息:</text>
        <text class="info-content">{{ debugInfo }}</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { get } from '../../utils/api'

const debugInfo = ref('点击按钮开始调试...')

const checkStorage = () => {
  try {
    const token = uni.getStorageSync('token')
    const userInfo = uni.getStorageSync('userInfo')
    
    debugInfo.value = `Token: ${token ? '存在' : '不存在'}\nUserInfo: ${userInfo ? '存在' : '不存在'}`
    
    if (token) {
      debugInfo.value += `\n\nToken值: ${token}`
    }
    
    if (userInfo) {
      debugInfo.value += `\n\nUserInfo: ${JSON.stringify(userInfo)}`
    }
  } catch (error) {
    debugInfo.value = `检查存储失败: ${error.message}`
  }
}

const clearStorage = () => {
  try {
    uni.removeStorageSync('token')
    uni.removeStorageSync('userInfo')
    debugInfo.value = '已清除本地存储'
  } catch (error) {
    debugInfo.value = `清除存储失败: ${error.message}`
  }
}

const testApiCall = async () => {
  try {
    debugInfo.value = '开始测试API调用...'
    
    // 检查本地是否有token
    const token = uni.getStorageSync('token')
    debugInfo.value += `\n本地token: ${token ? '存在' : '不存在'}`
    
    // 尝试调用API
    const response = await get('/api/cash-flow')
    debugInfo.value += `\nAPI调用成功: ${response.statusCode}`
    debugInfo.value += `\n数据: ${JSON.stringify(response.data)}`
  } catch (error) {
    debugInfo.value += `\nAPI调用失败: ${error.message}`
    console.error('API调用错误:', error)
  }
}
</script>

<style scoped>
.container {
  height: 100%;
  background-color: #f8f8f8;
  padding: 20px;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  color: #333;
}

.content {
  background-color: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.btn {
  width: 100%;
  height: 45px;
  background-color: #07C160;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  margin-bottom: 15px;
  cursor: pointer;
}

.btn:active {
  background-color: #06AD56;
}

.info-section {
  margin-top: 20px;
}

.info-title {
  display: block;
  font-weight: bold;
  margin-bottom: 5px;
  color: #333;
}

.info-content {
  display: block;
  padding: 10px;
  background-color: #f0f0f0;
  border-radius: 5px;
  word-break: break-all;
  white-space: pre-wrap;
}
</style>